﻿using SharpDX.XAudio2;

namespace Padsurface.Audio.Wave
{
    public class AudioEngine
    {
        private static AudioEngine _instance;
        private static readonly object _lock = new object();

        private XAudio2 _xaudio2;
        private MasteringVoice _masteringVoice;

        private AudioEngine()
        {
            _xaudio2 = new XAudio2();
            _masteringVoice = new MasteringVoice(_xaudio2);
        }

        public XAudio2 XAudio2
        {
            get
            {
                return _xaudio2;
            }
        }

        public MasteringVoice MasteringVoice
        {
            get
            {
                return _masteringVoice;
            }
        }

        public static AudioEngine GetInstance()
        {
            lock (_lock)
            {
                if (_instance == null)
                    _instance = new AudioEngine();
            }

            return _instance;
        }

        public void Destroy()
        {
            lock (_lock)
            {
                if (_masteringVoice != null)
                    _masteringVoice.Dispose();

                if (_xaudio2 != null)
                    _xaudio2.Dispose();

                _instance = null;
            }
        }
    }
}